#coding:utf-8
import sys
from PyQt5 import uic
from PyQt5.QtWidgets  import QApplication
from PyQt5.QtWidgets import QMainWindow

class Calc(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi('calc.ui',self)

        self.num_one = None
        self.num_two = None
        self.result = None
        self.operator = None
        self.equal = None
        self.text_result.setText('0')

        buttons = [
            self.pushButton0,
            self.pushButton1,
            self.pushButton2,
            self.pushButton3,
            self.pushButton4,
            self.pushButton5,
            self.pushButton6,
            self.pushButton7,
            self.pushButton8,
            self.pushButton9,
            self.pushButton_clear,
            self.pushButton_add,
            self.pushButton_sub,
            self.pushButton_div,
            self.pushButton_mul,
            self.pushButton_dot,
            self.pushButton_equal
        ]

        # for 循环 和map函数一样的效果
        # for button in  buttons :
        #     button.clicked.connect(self.clickedButton)
        # map()函数, lambda x : x + 2  匿名函数
        map(lambda button : button.clicked.connect(self.clickedButton),buttons)
      
    def calculate(self):

        # if self.operator :

        if self.num_one and self.num_two and self.operator :
            #eval() 函数是将字符串中的运算计算，其结果是数字
            if  self.num_two == '0' and self.operator == '/' :
                result = '无效计算'
                self.text_result.setText(result)
                self.num_one = None

            else:
                #运行计算

                if self.operator == '/':
                    # self.result = str(float(eval('%s%s%s'%(self.num_one,self.operator,self.num_two))))

                    #不保留几位小数的设置，就会自动保留6位小数
                    self.result = str('%f'%(float(self.num_one) / float(self.num_two)))
                else:
                    self.result = str(eval('%s%s%s'%(self.num_one,self.operator,self.num_two)))
                self.text_result.setText(self.result)
                self.num_one = self.result
 
        self.num_two = None
        # self.equal = None
        self.operator = None
            

    def  clear(self):
        self.num_one = None
        self.num_two = None
        self.result = None
        self.operator = None
        self.text_result.setText('0')


    def clickedButton(self):
        value = self.sender().text()
        #点击的数字
        if value in '0123456789.' :
            #没有找到限制最大显示个数

            if self.operator is None :
                if self.equal :
                    self.num_one = None
                    self.equal = None
                if self.num_one is None :
                    # 没值的小数点
                    if value == '.' :
                        self.num_one = '0.'
                    else:
                        self.num_one = value
                else :
                    if self.num_one == '0' and value == '0' :
                        self.num_one = value
                    else:
                        if value in self.num_one and value == '.' :
                            pass
                        else :
                            if self.num_one == '0':
                                if value == '.' :
                                    self.num_one = '0.'
                                else:
                                    self.num_one = value
                            else:
                                self.num_one += value 
                self.text_result.setText(self.num_one)
            else :
                if self.num_two is None:
                    # 没值的小数点
                     if value == '.' :
                        self.num_two = '0.'
                     else :
                        self.num_two = value
                else:
                    if self.num_two == '0' and value == '0':
                        self.num_two = value
                    else:
                        if value in self.num_two and value == '.' :
                            pass
                        else :
                            if self.num_two == '0':
                                if value == '.' :
                                    self.num_two = '0.'
                                else :
                                    self.num_two = value
                            else:
                                self.num_two += value
                self.text_result.setText(self.num_two)  
        elif value in '+-*/':

            if self.operator and self.num_two:
                self.calculate()
            
            self.operator = value

            self.equal =None
        elif value == '=':
            if self.equal :
                pass
            else:
                self.equal = value
                self.calculate()
        elif value == 'C':
            self.clear()

    
if __name__ == '__main__':    
    app = QApplication(sys.argv)
    calc = Calc()
    calc.show()
    app.exec_()




